package cn.agiledata.bank.myspace.service;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

import cn.agiledata.bank.common.service.MultiStatus;
import cn.agiledata.bank.common.util.StringUtil;

public class AuthenticationStatus implements MultiStatus {
    private static final String ENTRY_SEPARATOR = ",";

    private static final String KEY_VALUE_SEPARATOR = "=";

    /**
     * 根状态名称长度
     */
    public static final int ROOT_STATUS_NAME_LENGTH = 1;

    /**
     * 第二级状态名称长度
     */
    public static final int SECOND_LAYER_STATUS_NAME_LENGTH = 1;

    private Map statuses;

    private AuthenticationStatus() {
        this.statuses = new LinkedHashMap();
    }

    /**
     * 根据指定字符串生成认证状态
     * 
     * @param value
     *            e.g. "1=0, 2=1, 3A=2"
     * @return 认证状态
     */
    public static AuthenticationStatus getInstance(String value) {
        AuthenticationStatus as = new AuthenticationStatus();
        if (value != null && value.length() > 0) {
            String[] entries = value.split(ENTRY_SEPARATOR);
            for (int i = 0; i < entries.length; i++) {
                String entry = entries[i].trim();
                int index = entry.indexOf(KEY_VALUE_SEPARATOR);
                if (index > 0) {
                    String name = entry.substring(0, index);
                    String status = (entry.length() == index + 1) ? "" : entry
                            .substring(index + 1);
                    as.statuses.put(name, status);
                }
            }
        }
        return as;
    }

    /**
     * 根据指定状态名称和状态值，获取状态条目表达式
     * 
     * @param name
     *            状态名称
     * @param status
     *            状态值
     * @return 状态条目表达式
     */
    public static String getEntryExpression(String name, String status) {
        return name + KEY_VALUE_SEPARATOR + status;
    }

    public String getStatus(String name) {
        String status = null;
        for (Iterator it = this.statuses.entrySet().iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            String key = (String) entry.getKey();
            if (key.startsWith(name))
                status = (String) entry.getValue();
        }
        if (status == null)
            status = "";
        return status;
    }

    public void setStatus(String name, String status) {
        if (status == null) {
            status = "";
        } else {
            status = status.trim();
        }
        for (Iterator it = this.statuses.entrySet().iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            String key = (String) entry.getKey();
            if (key.startsWith(name)) {
                entry.setValue(status);
                return;
            }
            if (name.startsWith(key)
                    || StringUtil.sameStart(key, name).length() == ROOT_STATUS_NAME_LENGTH) {
                this.statuses.remove(key);
                this.statuses.put(name, status);
                return;
            }
        }
        this.statuses.put(name, status);
    }

    /**
     * 获取实际的状态名称
     * 
     * @param name
     *            状态名称
     * @return 实际的状态名称
     */
    public String getActualName(String name) {
        for (Iterator it = this.statuses.entrySet().iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            String key = (String) entry.getKey();
            if (key.startsWith(name)) {
                return key;
            }
        }
        return name;
    }

    public String toString() {
        String s = "";
        for (Iterator it = this.statuses.entrySet().iterator(); it.hasNext();) {
            Map.Entry entry = (Map.Entry) it.next();
            String key = (String) entry.getKey();
            String value = (String) entry.getValue();
            s += key + KEY_VALUE_SEPARATOR + value + ENTRY_SEPARATOR + " ";
        }
        return s.substring(0, s.length() - 2);
    }

}
